package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DiXiaoNie
 * @since 2022/12/24 17:13
 */
@Service
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {

    @Resource
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Resource
    private ArticleHomeMapper articleHomeMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 构建文章minio
     *
     * @param apArticle
     * @param content   内容
     * @author DiXiaoNie
     * @date 2022/12/24
     */
    @Override
    @Async
    public void buildArticleToMinio(ApArticle apArticle, String content) {
        try {
            //1.获取文章内容
            ApArticleContent apArticleContent = this.articleContentMapper.selectOne(
                    Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId())
            );

            //2.获取模板
            Template template = configuration.getTemplate("article.ftl");
            Map params = new HashMap<>();
            params.put("content", JSONArray.parseArray(content));
            //声明空的字符串写入流
            StringWriter stringWriter = new StringWriter();


            //3.生成静态html文件
            template.process(params, stringWriter);

            //4.上传到minio中
            InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
            String path = fileStorageService.uploadHtmlFile("", apArticleContent.getArticleId() + ".html", inputStream);


            //5.修改表中url字段的内容

            apArticle = this.articleHomeMapper.selectById(apArticleContent.getArticleId());
            if (apArticle != null) {
                apArticle.setStaticUrl(path);
                articleHomeMapper.updateById(apArticle);
            }

            //发送消息
            SearchArticleVo vo = new SearchArticleVo();
            BeanUtils.copyProperties(apArticle, vo);
            vo.setLayout(Integer.valueOf(apArticle.getLayout()));
            vo.setContent(content);
            vo.setStaticUrl(path);
            kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
